define(function(require, exports, module) {

    var $ = require('$'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        MeetingModel = require('model/plan'),
        MeetingView = require('view/plan'),
        MeetingCollection = require('collection/plan');

    module.exports = Backbone.View.extend({
        el: '.page[data-page=home]',
        events: { },
        meetingList: new MeetingCollection,
        initialize: function(options) {
            //传入gomac
            this.gomac = options.gomac;
        },
        render: function() {
            var self = this;
            //获取数据
            this.meetingList.fetch({
                success: function(collection, response, options) {
                    if( collection.length <= 0 ) { //如果暂时无数据
                        //TODO: 从远程获取
                        //弹出添加窗口
                        self.gomac.popup('.popup-meeting');
                        self.gomac.alert('请添加第一个活动');
                    }
                    else {
                        _.each(collection.models, function(model) {
                            self.appendOne(model);
                        });
                    }
                },
                error: function(collection, response, options) {
                    //错误发生了
                    self.gomac.alert('获取数据时错误发生了');
                    console.log(response);
                }
            });
        },
        appendOne: function(meeting) {
            var self = this;
            var view = new MeetingView({
                model: meeting,
                gomac: self.gomac
            });
            if(meeting.time.isEnded()) { //已经结束
                self.$el.find('#history ul').prepend( view.render().el );
            }
            else if(meeting.time.isStarted()) { //正在进行
                self.$el.find('#active ul').append( view.render().el );
            }
            else { //即将开始
                self.$el.find('#coming ul').append( view.render().el );
            }
        },
        prependOne: function(meeting) {
            var self = this;
            var view = new MeetingView({
                model: meeting,
                gomac: self.gomac
            });
            if(meeting.time.isEnded()) { //已经结束
                self.$el.find('#history ul').prepend( view.render().el );
            }
            else if(meeting.time.isStarted()) { //正在进行
                self.$el.find('#active ul').prepend( view.render().el );
            }
            else {
                self.$el.find('#coming ul').prepend( view.render().el );
            }
        }
    });

});